package com.project.keysManager;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QRCodeDecoder {
    public String decode(File imageFile) {
        BufferedImage image;
        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e1) {
            return "Image failed";
        }

        // creating luminance source
        LuminanceSource lumSource = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource));

        // barcode decoding
        Reader reader = new QRCodeReader();

        Result result = null;
        try {
            result = reader.decode(bitmap);
        } catch (ReaderException e) {
            return "Reader error";
        }

        return result.getText();

    }
}
